package xhwl.hmo.mall.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import de.greenrobot.event.EventBus;
import xhwl.hmo.mall.BaseActivity;
import xhwl.hmo.mall.Constant;
import xhwl.hmo.mall.IntentConstants;
import xhwl.hmo.mall.R;
import xhwl.hmo.mall.bean.TZConfirmOrder;
import xhwl.hmo.mall.event.PaymentFinishEvent;
import xhwl.hmo.mall.http.api.AddressRecentApi;
import xhwl.hmo.mall.http.api.BuyApi;
import xhwl.hmo.mall.http.bean.resp.AddressListResp;
import xhwl.hmo.mall.http.bean.resp.BuyResp;
import xhwl.hmo.mall.http.bean.resp.HttpResp;
import xhwl.hmo.mall.utils.AlarmUtil;
import xhwl.hmo.mall.utils.AppUtil;
import xhwl.hmo.mall.utils.SpUtil;
import xhwl.hmo.mall.utils.StringUtil;
import xhwl.hmo.mall.utils.ToastUtil;
import xhwl.hmo.mall.utils.XHLog;
import xhwl.retrofitrx.HttpManager;
import xhwl.retrofitrx.OnNextListener;


/**
 * Created by admin on 2017-10-25.
 */

public class ConfirmOrderActivity extends BaseActivity implements View.OnClickListener {
    private static final int REQUEST_CODE_ADDR=0;
    public static final String EXTRA_DATA="data";
    private LinearLayout llSelectAddress,llEmptyAddress;
    private android.widget.TextView tvProductName;
    private android.widget.TextView tvProductDesc;
    private android.widget.TextView tvPrice;
    private android.widget.TextView tvPriceOrigin;
    private android.widget.TextView tvPersonName;
    private android.widget.TextView tvPersonPhone;
    private android.widget.TextView tvPersonCardNo;
    private android.widget.TextView tvOrderNo;
    private android.widget.TextView tvOrderTime;
    ImageView ivGoods;
    TextView tvPayMoney;
    Button btnPay;
    TextView tvAddressPersonName,tvAddress,tvDetailAddress,tvPersonAddressPhone;
    TZConfirmOrder confirmOrder;
    AddressListResp.Address addr;//当前选择的地址
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        confirmOrder= (TZConfirmOrder) getIntent().getSerializableExtra(EXTRA_DATA);
        setContentView(R.layout.activity_confirm_order);
    }

    @Override
    public void initViews() {
        initTitleViews();
        this.tvAddressPersonName= (TextView) findViewById(R.id.tvAddressPersonName);
        this.tvAddress= (TextView) findViewById(R.id.tvAddress);
        this.tvDetailAddress= (TextView) findViewById(R.id.tvDetailAddress);
        this.tvPersonAddressPhone= (TextView) findViewById(R.id.tvPersonAddressPhone);

        this.tvPayMoney=(TextView) findViewById(R.id.tvPayMoney);
        this.btnPay= (Button) findViewById(R.id.btnPay);
        this.ivGoods= (ImageView) findViewById(R.id.ivGoods);
        this.tvOrderTime = (TextView) findViewById(R.id.tvOrderTime);
        this.tvOrderNo = (TextView) findViewById(R.id.tvOrderNo);
        this.tvPersonCardNo = (TextView) findViewById(R.id.tvPersonCardNo);
        this.tvPersonPhone = (TextView) findViewById(R.id.tvPersonPhone);
        this.tvPersonName = (TextView) findViewById(R.id.tvPersonName);
        this.tvPriceOrigin = (TextView) findViewById(R.id.tvPriceOrigin);
        this.tvPrice = (TextView) findViewById(R.id.tvPrice);
        this.tvProductDesc = (TextView) findViewById(R.id.tvProductDesc);
        this.tvProductName = (TextView) findViewById(R.id.tvProductName);
        this.llEmptyAddress = (LinearLayout) findViewById(R.id.llEmptyAddress);
        this.llSelectAddress = (LinearLayout) findViewById(R.id.llSelectAddress);
        this.tvOrderTime.setVisibility(View.GONE);
        this.tvOrderNo.setVisibility(View.GONE);
        llEmptyAddress.setVisibility(View.VISIBLE);
        llSelectAddress.setVisibility(View.GONE);

        initData();

    }

    @Override
    protected void setUpTitleView() {
        super.setUpTitleView();
        titleTextView.setText("填写订单");
    }

    @Override
    public void initListener() {
        this.btnPay.setOnClickListener(this);
        this.llEmptyAddress.setOnClickListener(this);
        this.llSelectAddress.setOnClickListener(this);
    }
    private void initData(){
        if(confirmOrder==null)return;
        this.tvProductName.setText(confirmOrder.goodName);
        this.tvPriceOrigin.setText("￥"+confirmOrder.price+"/年");
        this.tvPrice.setText("￥"+confirmOrder.salePrice+"/年");
        this.tvPayMoney.setText("￥"+confirmOrder.salePrice);
        Glide.with(this).load(confirmOrder.picUrl).into(this.ivGoods);
        this.tvPersonName.setText(confirmOrder.personName);
        this.tvPersonPhone.setText(confirmOrder.personMobile);
        String idCardNo=confirmOrder.personIDCard;
        if(StringUtil.isEmpty(idCardNo)){
            this.tvPersonCardNo.setText("暂未填写");
        }
        else
        {
            this.tvPersonCardNo.setText(confirmOrder.personIDCard);
        }

    }
    private void initAddrData(){
        llEmptyAddress.setVisibility(View.GONE);
        llSelectAddress.setVisibility(View.VISIBLE);
        tvAddressPersonName.setText("收货人："+addr.SH_NAME);
        tvAddress.setText(addr.PROVINCE_NAME+" "+addr.CITY_NAME+" "+addr.DISTRICT_NAME);
        tvDetailAddress.setText(addr.SH_ADDRESS);
        tvPersonAddressPhone.setText(addr.SH_MOBILE);
    }
    @Override
    public void loadData() {
        getRecentAddr();
    }

    private void getRecentAddr() {
        String member_mobile=SpUtil.getString(SpUtil.MEMBER_MOBILE,"");
        AddressRecentApi api=new AddressRecentApi(member_mobile);
        HttpManager.getInstance().dealHttp(this, api, new OnNextListener<HttpResp<AddressListResp.Address>>() {
            @Override
            public void onNext(HttpResp<AddressListResp.Address> resp) {
                if(resp==null||resp.data==null||resp.data==null){
                    llEmptyAddress.setVisibility(View.VISIBLE);
                    llSelectAddress.setVisibility(View.GONE);
                }else
                {
                    if(resp.isOk()){
                        addr=resp.data;
                        XHLog.e(addr.toString());
                        initAddrData();
                    }else{
                        llEmptyAddress.setVisibility(View.VISIBLE);
                        llSelectAddress.setVisibility(View.GONE);
                    }
                }
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btnPay:
                if(addr==null|| StringUtil.isEmpty(addr.RECORD_ID)){
                    buy("");
                }
                else
                {
                    buy(addr.RECORD_ID);
                }
                break;
            case R.id.llEmptyAddress:
            case R.id.llSelectAddress:
                Intent intent=new Intent(this,AddressManageActivity.class);
                startActivityForResult(intent,REQUEST_CODE_ADDR);

                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==REQUEST_CODE_ADDR&&resultCode==RESULT_OK){
            if(data!=null){
                addr= (AddressListResp.Address) data.getSerializableExtra("data");
                initAddrData();
            }
        }
    }

    private void buy(String addressId){
        String memberMobile= SpUtil.getString(SpUtil.MEMBER_MOBILE,"");
        BuyApi.BuyReq req=new BuyApi.BuyReq(memberMobile,confirmOrder.personName,confirmOrder.personMobile
                ,confirmOrder.personIDCard,confirmOrder.sex,confirmOrder.age,confirmOrder.saleType,confirmOrder.salePrice,addressId);
        BuyApi api=new BuyApi(req);
        HttpManager.getInstance().dealHttp(this, api, new OnNextListener<HttpResp<BuyResp>>() {
            @Override
            public void onNext(HttpResp<BuyResp> resp) {
                if(resp.isOk()){
                    String orderId=resp.data.order_id;
                    //开启定时任务
                    Intent intent = new Intent(IntentConstants.Action.orderRemind);
                    intent.putExtra(IntentConstants.EXTRA_REMIND_ORDER_ID, orderId);
//                    intent.putExtra(IntentConstants.EXTRA_GOODS_TYPE,  GoodType.TZ);
                    long triggerAtTime = System.currentTimeMillis()  +  Constant.REMIND_ORDER_TIME;
                    AlarmUtil.controlAlarm(mContext, triggerAtTime,Integer.parseInt(orderId), intent);

                    AppUtil.startPayment(mContext,orderId,confirmOrder.goodName,confirmOrder.salePrice);
                }
                else
                {
                    ToastUtil.showToast(mContext,resp.message);
                }
            }
        });
    }
    public void onEvent(PaymentFinishEvent event){
        finish();
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
